package zwatcher

import (
	"gitee.com/youkelike/ziam/zapp/app"
	"gitee.com/youkelike/ziam/zwatcher/config"
	"gitee.com/youkelike/ziam/zwatcher/options"
	"gitee.com/youkelike/zlog"
)

const commandDesc = `Zwatcher is a pluggable watcher service used to do some periodic work like cron job. 
But the difference with cron job is zwatcher also support sleep some duration after previous job done.`

func NewApp(basename string) *app.App {
	opts := options.NewOptions()
	application := app.NewApp("zwatch server",
		basename,
		app.WithOptions(opts),
		app.WithDescription(commandDesc),
		app.WithDefaultValidArgs(),
		app.WithRunFunc(run(opts)),
	)

	return application
}

func run(opts *options.Options) app.RunFunc {
	return func(basename string) error {
		zlog.Init(opts.Log)
		defer zlog.Sync()

		cfg, err := config.CreateConfigFromOptions(opts)
		if err != nil {
			return err
		}

		return Run(cfg)
	}
}
